<?php

namespace app\logic\im;

use app\logic\im\BaseLogic;
use app\common\model\User;
use app\admin\model\im\Account AS ImAccount;

// use app\common\enum\fp\PaymentType;
// use app\common\model\fp\PostAudit;
// use app\common\model\fp\PostIndex;
// use app\common\model\fp\PostMedia;

// use think\facade\Event;
// use think\facade\Log;

class UserSigLogic extends BaseLogic
{
    // 生成用户签名
    public function genUserSig($user_id, $expire = 86400)
    {
        $output = [
            'SDKAppID' => $this->im_config['sdkappid'],
            'userID' => $this->im_config['prefix'] . $user_id,
            'userSig' => $this->im()->genUserSig($user_id, $expire) // 一天有效期的令牌
        ];
        $this->import([$user_id]);
        $this->friendAdd($output['userID']);
        return $output;
    }

    // 导入用户
    public function import(array $user_id_arr)
    {
        $account_list = [];

        $user_model = User::initModel();
        $user_res = $user_model->whereIn('id', $user_id_arr)->select();
        if($user_res && count($user_res) > 0){
            foreach($user_res as $user){
                $im_user_id = $this->im_config['prefix'] . $user->id;
                $account_list[] = [
                    'UserID' => $im_user_id,
                    'Nick' => $user->nickname,
                    'FaceUrl' => $user->avatar
                ];

                $record = ImAccount::where(
                                [
                                    ['im_user_id', '=', $im_user_id],
                                    ['delete_time', '=', 0]
                                ]
                            )->find();

                $data = [
                    'type' => 'M',
                    'sdk_app_id' => $this->im_config['sdkappid'],
                    'im_user_id' => $im_user_id,
                    'user_id' => $user->id,
                    'nick' => $user->nickname,
                    'face_url' => $user->avatar
                ];

                if ($record) {
                    // 更新数据
                    $record->save($data);
                } else {
                    // 插入数据
                    (new ImAccount)->save($data);
                }
            }
        }
        $post_data = [
            'AccountList' => $account_list
        ];

        $path = 'v4/im_open_login_svc/multiaccount_import';

        return $this->postraw($path, $post_data);
    }

    // 添加好友
    public function friendAdd(string $from_account)
    {
        $post_data = [
            'From_Account' => $from_account, // 需要为该 UserID 添加好友
            'AddFriendItem' => [
                [
                    'To_Account' => $this->im_config['system_notifications_account'], // 好友的 UserID
                    'AddSource' => 'AddSource_Type_System'
                ]
            ],
            'AddType' => 'Add_Type_Both', // 加好友方式（默认双向加好友方式）： Add_Type_Single 表示单向加好友 Add_Type_Both 表示双向加好友
            'ForceAddFlags' => 1 // 管理员强制加好友标记： 1：表示强制加好友 0：表示常规加好友方式
        ];

        $path = 'v4/sns/friend_add';

        return $this->postraw($path, $post_data);
    }
}